package com.cliave.ttweb.controller;


import com.cliave.ttweb.bean.Dishes;
import com.cliave.ttweb.service.DishService;
import com.cliave.ttweb.vo.DishResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;


/**
 * 1,@Controller它是一个spring mvc的注解
 * 代表他是一个controller类，用来接收请求，生成相应
 * 2,@RequestMapping,也是注解，设置一个请求路径的意思
 * 3,@ResponseBody,自动把返回类型变成json格式，把返回对象放到返回类型里
 * 4,@RestController是@Controller加上@ResponseBody
 * 5,@CrossOrigin    跨域注解
 * 6.@Valid  JSR303检验，在dishes类里面设置了检验的是值非空
 */
@RestController
@RequestMapping("/dishController")
@CrossOrigin
public class DishController {
    //@Autowired会从spring的容器里面把这个接口的唯一子类，拿出来给它，所以我们就不用new对象了
    @Autowired
    private DishService dishService;

@RequestMapping("/addDishes")
    public DishResult addDishe(@RequestBody @Valid Dishes dishes){
    boolean b = dishService.addDishes(dishes);
    if (b){
        return DishResult.ok();
        }else {
        return DishResult.error("添加菜品失败");
        }

    }
    @RequestMapping("/findOne/{id}")
    public DishResult findOne(@PathVariable Integer id) {
        Dishes dishes = dishService.findDishById(id);
        return DishResult.ok().data("dishes",dishes);
    }
    @RequestMapping("/updateDish")
    public DishResult updateDish(@RequestBody @Valid Dishes dishes){
        boolean b = dishService.updateDish(dishes);
        if (b){
            return DishResult.ok();
        }else {
            return DishResult.error("修改菜品失败");
        }
    }
    @RequestMapping("/delete/{id}")
    public DishResult deleteDish(@PathVariable Integer id){
       boolean b = dishService.deleteDish(id);
        if (b){
            return DishResult.ok();
        }else {
            return DishResult.error("删除菜品失败");
        }
    }
}
